package graduate.cslg.com.universityshopping.card;

import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.CardResponse;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChangePasswordActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText originPassword;
    private EditText newPassword;
    private EditText ensurePassword;
    private Button submitButton;
    String origin;
    String newPass;
    String repeatPass;
    private String originPass;
    private String getUserAccount;
    private String getUserName;
    private String getUserPhone;
    private CardResponse mCardResponse;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        initViews();
        Toolbar toolbar = findViewById(R.id.toolbar);
        //添加toolbar支持
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        SharedPreferences preferences = this.getSharedPreferences("user_info",MODE_PRIVATE);
        getUserAccount = preferences.getString("user_account", "Z09315136");
        getUserName = preferences.getString("user_name", "Tom wang");
        getUserPhone = preferences.getString("user_phone", "110");
        getOrigin();
        submitButton.setOnClickListener(this);
    }

    private void initViews() {
        originPassword = findViewById(R.id.original_password);
        newPassword = findViewById(R.id.new_password);
        ensurePassword = findViewById(R.id.repeat_password);
        submitButton = findViewById(R.id.submit);
    }

    /**
     * 验证输入的信息是否正确
     * @param origin 原密码
     * @param newPass 新密码
     * @param surePass 重复密码
     * @return 正确返回true，错误则返回false
     */
    private boolean canPass(String origin, String newPass, String surePass) {
        return surePass.equals(newPass) && origin.equals(originPass);
    }

    private void getOrigin() {
        RetrofitDevelop.getAccountService()
                .getCardInfo(getUserAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CardResponse>() {
                    @Override
                    public void onCompleted() {
                        originPass = mCardResponse.getUser_password();
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(ChangePasswordActivity.this, "网络异常！", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(CardResponse cardResponse) {
                        mCardResponse = cardResponse;
                    }
                });
    }

    private void updatePass(String userAccount, String newPass, String userPhone, String userName) {
        RetrofitDevelop.getAccountService()
                .isUpdate(userAccount, newPass, userPhone, userName)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(ChangePasswordActivity.this, "密码修改成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ChangePasswordActivity.this, "修改失败，请稍后再试", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Toast.makeText(ChangePasswordActivity.this, "网络错误，请稍后再试！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit:
                origin = originPassword.getText().toString();
                newPass = newPassword.getText().toString();
                repeatPass = ensurePassword.getText().toString();
                if (canPass(origin, newPass, repeatPass)) {
                    updatePass(getUserAccount, newPass, getUserPhone, getUserName);
                } else {
                    Toast.makeText(ChangePasswordActivity.this, "输入有误，请检查！", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
